home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_cyclone.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  2.1 KB  |  75 lines

  1. #define TWOPI (2*PI)
  2.  
  3. /* Use signed Perlin noise */
  4. #define snoise(x) ((2*noise(x))-1)
  5. #define DNoise(p) (2*(point noise(p)) - point(1,1,1))
  6. #define VLNoise(Pt,scale) (snoise(DNoise(Pt)+(scale*Pt)))
  7. #define VERY_SMALL 0.001
  8.  
  9.  
  10. surface k3d_cyclone(float Ka = 0.5, Kd = 0.75; float max_radius = 1;
  11.             float twist = 0.5; float scale = .7, offset = .5;
  12.             float omega = 0.675; float octaves = 4;
  13.   )
  14. {
  15.   float radius, dist, angle, sine, cosine, eye_weight, value;
  16.   point Pt;            /* Point in texture space */
  17.   point PN;            /* Normalized vector in texture space */
  18.   point PP;            /* Point after distortion */
  19.   float l, o, a, i;        /* Loop control for fractal sum */
  20.  
  21.   /* Transform to texture coordinates */
  22.   Pt = transform("shader", P);
  23.  
  24.   /* Rotate hit point to "cyclone space" */
  25.   PN = normalize(Pt);
  26.   radius = sqrt(xcomp(PN) * xcomp(PN) + ycomp(PN) * ycomp(PN));
  27.  
  28.   if(radius < max_radius)
  29.     {                /* inside of cyclone */
  30.       /* invert distance from center */
  31.       dist = pow(max_radius - radius, 3);
  32.       angle = PI + twist * TWOPI * (max_radius - dist) / max_radius;
  33.       sine = sin(angle);
  34.       cosine = cos(angle);
  35.       PP =
  36.     point(xcomp(Pt) * cosine - ycomp(Pt) * sine,
  37.           xcomp(Pt) * sine + ycomp(Pt) * cosine, zcomp(Pt));
  38.       /* Subtract out "eye" of storm */
  39.       if(radius < 0.05 * max_radius)
  40.     {            /* if in "eye" */
  41.       eye_weight = (.1 * max_radius - radius) * 10;    /* normalize */
  42.       /* invert and make nonlinear */
  43.       eye_weight = pow(1 - eye_weight, 4);
  44.     }
  45.       else
  46.     eye_weight = 1;
  47.     }
  48.   else
  49.     PP = Pt;
  50.  
  51.   if(eye_weight > 0)
  52.     {                /* if in "storm" area */
  53.       /* Compute VLfBm */
  54.       l = 1;
  55.       o = 1;
  56.       a = 0;
  57.       for(i = 0; i < octaves && o >= VERY_SMALL; i += 1)
  58.     {
  59.       a += o * VLNoise(PP * l, 1);
  60.       l *= 2;
  61.       o *= omega;
  62.     }
  63.       value = abs(eye_weight * (offset + scale * a));
  64.     }
  65.   else
  66.     value = 0;
  67.  
  68.   /* Thin the density of the clouds */
  69.   Oi = value * Os;
  70.  
  71.   /* Shade like matte, but with color scaled by cloud opacity */
  72.   Ci =
  73.     Oi * Cs * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N), I)));
  74. }
  75.